// 管理首页相关数据的 vuex 子模块
import {
  reqCategoryList,
  reqBannerList,
  reqFloors,
  reqRecommends
} from '@/api'

const state = {
  categoryList: [],
  bannerList: [],
  recommends: [],
  floors: []
}

const mutations = {
  // 接收保存分类列表
  RECEIVE_CATEGORY_LIST (state, categoryList) {
    state.categoryList = categoryList.splice(0, 15)
  },
  // 接收保存广告轮播列表
  RECEIVE_BANNER_LIST (state, bannerList) {
    state.bannerList = bannerList
  },
  // 接收今日推荐列表
  RECEIVE_RECOMMENDS (state, recommends) {
    state.recommends = recommends
  },
  // 接收楼层列表
  RECEIVE_FLOORS (state, floors) {
    state.floors = floors
  }
}

const actions = {
  // 获取三级分类列表的异步 action
  async getCategoryList ({ commit }) {
    const result = await reqCategoryList()
    if (result.code === 200) {
      const categoryList = result.data
      commit('RECEIVE_CATEGORY_LIST', categoryList)
    }
  },
  // 获取广告轮播列表异步 action
  async getBannerList ({ commit }) {
    // 发送异步 ajax 请求(调用接口请求函数)
    const result = await reqBannerList()
    if (result.code === 200) {
      const bannerList = result.data
      commit('RECEIVE_BANNER_LIST', bannerList)
    }
  },
  // 获取今日推荐列表
  async getRecommends ({ commit }) {
    const result = await reqRecommends()
    if (result.code === 200) {
      const recommends = result.data
      commit('RECEIVE_RECOMMENDS', recommends)
    }
  },
  // 获取楼层列表
  async getFloors ({ commit }) {
    const result = await reqFloors()
    if (result.code === 200) {
      const floors = result.data
      commit('RECEIVE_FLOORS', floors)
    }
  }
}

const getters = {

}


export default {
  state,
  mutations,
  actions,
  getters
}